<?php
require_once "rest-request.php";

interface IRestStrategy {
	public function setRequest($url, $username = "", $password = "", $args = null);
    public function execute();
}
 
class GetRequest implements IRestStrategy {
	
	private $reqUtil = null;
	
	public function setRequest($url, $username = "", $password = "", $args = null) {
		$this->reqUtil =& new HttpRequest($url);
		if ($this->user_name != "" && $this->password != "") {
           $this->reqUtil->setAuthentication($username, $password);
        }
        $this->reqUtil->setRequestMethod(HTTP_REQUEST_METHOD_GET);
	}
	
    public function execute() {
        $this->reqUtil->sendRequest();
        return $this->reqUtil->getResponse();
    }
}
 
class PostRequest implements IRestStrategy {
	
	private $reqUtil = null;
	
	public function setRequest($url, $username = "", $password = "", $args = null) {
		$this->reqUtil =& new HttpRequest($url);
		if ($this->user_name != "" && $this->password != "") {
           $this->reqUtil->setAuthentication($username, $password);
        }
        $this->reqUtil->setRequestMethod(HTTP_REQUEST_METHOD_POST);
        $this->reqUtil->setRequestData($args);
	}
	
    public function execute() {
        $this->reqUtil->sendRequest();
        return $this->reqUtil->getResponse();
    }
}
 
class PutRequest implements IRestStrategy {
	
	private $reqUtil = null;
	
	public function setRequest($url, $username = "", $password = "", $args = null) {
		$this->reqUtil =& new HttpRequest($url);
		if ($this->user_name != "" && $this->password != "") {
           $this->reqUtil->setAuthentication($username, $password);
        }
        $this->reqUtil->setRequestMethod(HTTP_REQUEST_METHOD_PUT);
        $this->reqUtil->setRequestData($args);
	}
	
    public function execute() {
        $this->reqUtil->sendRequest();
        return $this->reqUtil->getResponse();
    }
}

class DeleteRequest implements IRestStrategy {
	
	private $reqUtil = null;
	
	public function setRequest($url, $username = "", $password = "", $args = null) {
		$this->reqUtil =& new HttpRequest($url);
		if ($this->user_name != "" && $this->password != "") {
           $this->reqUtil->setAuthentication($username, $password);
        }
        $this->reqUtil->setRequestMethod(HTTP_REQUEST_METHOD_DELETE);	
	}
	
	public function execute() {
		$this->reqUtil->sendRequest();
        return $this->reqUtil->getResponse();
	}
}
 
class RESTContext {
    var $strategy;
     
    private $response = "";
    private $responseBody = "";
    private $isRequestSet = false;

    public function __construct(IRestStrategy $strategy) {
        $this->strategy = $strategy;
    }
    
    public function setRequest($url = "", $inputs = null, $username = "", $password = "") {
    	$this->strategy->setRequest($url, $inputs, $username, $password);
    	$this->isRequestSet = true;
    }
 
    public function execute() {
    	if($this->isRequestSet) {
    		return $this->strategy->execute();
    	} else {
    		// throw an exception	
    	}
    }
}

?>
